package com.joe.mp4_demo.controller;

import com.joe.mp4_demo.dto.BaseImgDTO;
import com.joe.mp4_demo.utils.Base;
import com.joe.mp4_demo.utils.DownLoadUtil;
import com.joe.mp4_demo.utils.FFmepgUtils;
import com.joe.mp4_demo.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
@RequestMapping(value = "/img")
@Api(value = "图片接口",description = "图片接口")
@Slf4j
public class ImgController {

    @ApiOperation(value = "将图片地址[Base64]传入合成视频",notes = "将图片地址[Base64]传入合成视频")
    @RequestMapping(value = "/doNew",method = RequestMethod.POST)
    public Result doNew(@RequestBody BaseImgDTO dto){
        Result result = checkDTO(dto);
        if (result.getCode()!=200){
            return result;
        }
        log.info("图片文件数量:{}",dto.getFileList().size());
        //TODO 1.图片Base64转.jpeg文件并放临时文件夹
        Base.doList2(dto.getFileList());
        log.info("图片生成完成");
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String localDate = LocalDateTime.now().format(ofPattern);
        String mp4SavePath = "C:/Users/Administrator/Desktop/test/"+localDate+".mp4";
        //TODO 2.将临时文件夹中图片转.mp4视频
        FFmepgUtils.doFFe(mp4SavePath);
        log.info("视频转换完成");
        //TODO 3.视频转换完成后删除临时文件夹
        Base.delete();
        return Result.success(mp4SavePath);
    }

    @ApiOperation(value = "根据视频名称下载视频",notes = "根据视频名称下载视频")
    @RequestMapping(value = "/downLoad/{fileName}",method = RequestMethod.GET)
    public void downLoad(@PathVariable("fileName") String fileName, HttpServletResponse response) throws Exception {
        String path1 = "C:/Users/Administrator/Desktop/test/";
        DownLoadUtil.downloadLocal(path1 , fileName, response);
    }

    public Result checkDTO(BaseImgDTO dto){
        List<String> fileList = dto.getFileList();
        if (fileList==null || fileList.size()==0){
            return Result.fail("图片不能为空");
        }
        return Result.success();
    }


}
